package cn.laixueit.service.impl;

import cn.laixueit.mapper.CategoryMapper;
import cn.laixueit.pojo.Category;
import cn.laixueit.pojo.CategoryExample;
import cn.laixueit.service.CategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<Category> list(Integer pid,String name) {
        CategoryExample example = getCondition(new Category(null,name,pid));
        return categoryMapper.selectByExample(example);
    }

    @Override
    public void save(Category category) {
        categoryMapper.insertSelective(category);
    }

    @Override
    public Category findById(Integer id) {
        return categoryMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(Category category) {
        categoryMapper.updateByPrimaryKeySelective(category);
    }

    @Override
    public Category delById(Integer id) {
        // 先查询是否存在下级分类
       /* CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentidEqualTo(id);*/

        CategoryExample example = getCondition(new Category(null, null, id));
        List<Category> categoryList = categoryMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(categoryList)){
            // 删除
            categoryMapper.deleteByPrimaryKey(id);
            return null;
        }
        return categoryList.get(0);
    }

    /**
     *
     * @return  集合类型map，包含一级分类下的二级及三级分类
     */
    public List<Map<String, Object>> findCategoryListMap() {
        Category category = new Category();
        category.setParentid(0);
        CategoryExample example = getCondition(category);
        List<Category> category1List = categoryMapper.selectByExample(example);
        List<Map<String, Object>> mapList = findCategoryChildrenListByParentId(category1List);
        return mapList;
    }

    private List<Map<String,Object>> findCategoryChildrenListByParentId(List<Category> categories){
        // 最终返回的listMap
        ArrayList<Map<String, Object>> category1ListMap = new ArrayList<>();

        // 遍历 一级分类
        for (Category category : categories) {

            // 得到一级分类的id
            Integer id = category.getId();

            // 组件一级分类的返回值
            HashMap<String, Object> map1 = new HashMap<>();
            map1.put("value",id);
            map1.put("label",category.getName());


            // 组件二级菜单的查询条件
            Category c = new Category();
            c.setParentid(id);
            CategoryExample example = getCondition(c);
            // 查询出一级分类下的二级分类
            List<Category> category2List = categoryMapper.selectByExample(example);

            // 一级的children
            ArrayList<Map<String, Object>> category2ListMap = new ArrayList<>();
            // 遍历二级分类
            for (Category category2 : category2List) {

                // 组件二级分类的返回值
                HashMap<String, Object> map2 = new HashMap<>();
                map2.put("value",category2.getId());
                map2.put("label",category2.getName());

                // 组件三级分类的查询条件
                Category c2 = new Category();
                c2.setParentid(category2.getId());
                CategoryExample example2 = getCondition(c2);
                // 查询出二级分类下的三级分类
                List<Category> category3List = categoryMapper.selectByExample(example2);

                // 三级分类的返回值，二级分类的children
                ArrayList<Map<String, Object>> category3ListMap = new ArrayList<>();
                // 遍历二级下的三级分类
                for (Category category3 : category3List) {
                    // 组件返回值
                    HashMap<String, Object> map3 = new HashMap<>();
                    map3.put("value",category3.getId());
                    map3.put("label",category3.getName());
                    // 将组件的三级分类map放入二级的children中对象
                    category3ListMap.add(map3);
                }
                // 二级分类对象的children
                map2.put("children",category3ListMap);
                // 二级的list的每一个children中对象
                category2ListMap.add(map2);
            }
            // 一级分类的childre中对象
            map1.put("children",category2ListMap);
            // 将一级对象放入 一级list
            category1ListMap.add(map1);
        }

        return category1ListMap;
    }


    private CategoryExample getCondition(Category category){
        CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        if(category != null){
            if(category.getId()!=null){
                criteria.andIdEqualTo(category.getId());
            }
            if(category.getName()!=null && !StringUtils.isBlank(category.getName())){
                criteria.andNameLike("%"+category.getName()+"%");
            }
            if(category.getParentid()!=null){
                criteria.andParentidEqualTo(category.getParentid());
            }
        }
        return example;
    }
}
